package com.kakavr.emp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.shop.service.IShopAdminService;
import com.kakavr.user.service.IUserService;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;
/**
 * 店铺信息
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("shop")
public class ShopController extends BaseController {
	@Resource
	private IShopAdminService shopAdminService;
	@Resource
	private IUserService userService;

	/**
	 * 店铺信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectShop", method = RequestMethod.GET)
	public Map<String, Object> selectShop(HttpServletRequest request) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("storeId", SessionUtils.getShopId());
		return MsgResult.okResultData(shopAdminService.getShopInfo(map));
	}

	/**
	 * 完善支付宝帐号
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateShop", method = RequestMethod.POST)
	public Map<String, Object> updateShop(HttpServletRequest request) {
		int isMaster = (int) SessionUtils.getCurUser().get("isMaster");
		if (isMaster > 0) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2005,
					MsgConstants.MSG_CONTENT_2005);
		}
		String payeeAccount = request.getParameter("payeeAccount");
		if (StringUtils.isBlank(payeeAccount)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "支付宝账户");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", SessionUtils.getShopId());
		map.put("payeeAccount", payeeAccount);
		shopAdminService.updateShop(map);
		return MsgResult.okResult();
	}

	/**
	 * 修改店铺密码
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updatePass", method = RequestMethod.POST)
	public Map<String, Object> updatePass(HttpServletRequest request) {
		String oldPass = request.getParameter("oldPass");
		if (StringUtils.isBlank(oldPass)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "原密码");
		}
		String password = request.getParameter("password");
		if (StringUtils.isBlank(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "店铺登录密码");
		}
		if (!ValidatorUtils.isPass(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "店铺登录密码");
		}
		if (oldPass.equals(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2003,
					MsgConstants.MSG_CONTENT_2003);
		}
		Map<String, Object> user = SessionUtils.getCurUser();
		int isMaster = (int) user.get("isMaster");
		if (isMaster > 0) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2005,
					MsgConstants.MSG_CONTENT_2005);
		}
		String pass = (String) user.get("shopPass");
		if (oldPass.equals(pass) || EncryptionUtils.md5(oldPass).equals(pass)) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", user.get("shopId"));
			map.put("password", EncryptionUtils.md5(password));
			shopAdminService.updateShop(map);
			return MsgResult.okResult();
		}
		throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
				MsgConstants.MSG_CONTENT_1002, "原密码");
	}

	/**
	 * 添加玩家
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addUser", method = RequestMethod.POST)
	public Map<String, Object> addUser(HttpServletRequest request) {
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		if (StringUtils.isBlank(userName)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "用户名");
		}
		if (!ValidatorUtils.isPass(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "密码");
		}
		String times = request.getParameter("times");
		if (!ValidatorUtils.isNumber(times)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "次数");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("username", userName);
		map.put("times", times);
		map.put("isActivity", 1);
		map.put("storeId", SessionUtils.getShopId());
		map.put("password", EncryptionUtils.md5(password));
		userService.addUserByRegister(map);
		return MsgResult.okResult();
	}

}
